package com.gitee.target123.develop.pay;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.gitee.target123.develop.pay.ali.AliPay;
import com.gitee.target123.develop.pay.ali.AliPayMap;
import com.gitee.target123.develop.pay.ali.AliPayProperties;
import com.gitee.target123.develop.pay.wx.WxPay;
import com.gitee.target123.develop.pay.wx.WxPayMap;
import com.gitee.target123.develop.pay.wx.WxPayProperties;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
@EnableConfigurationProperties({PayMapProperties.class,PaySingleProperties.class})
public class PayAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(name = "pay.single.wx-enabled",havingValue = "true")
    public WxPay wxPay(PaySingleProperties paySingleProperties){
        WxPay wxPay = new WxPay();
        WxPayProperties properties = paySingleProperties.getWx();
        wxPay.setWxPayProperties(properties);

        WxPayConfig wxPayConfig = new WxPayConfig();
        wxPayConfig.setAppId(properties.getAppId());
        wxPayConfig.setMchId(properties.getMchId());
        wxPayConfig.setMchKey(properties.getMchKey());
        wxPayConfig.setKeyPath(properties.getKeyPath());
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(wxPayConfig);

        wxPay.setWxPayService(wxPayService);
        return wxPay;
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(name = "pay.map.wx-enabled",havingValue = "true")
    public WxPayMap wxPayMap(PayMapProperties payMapProperties){
        WxPayMap wxPayMap = new WxPayMap();
        Map<String, WxPayProperties> wx = payMapProperties.getWx();
        Map<String, WxPay> map = new HashMap<>(wx.size());
        wx.forEach((s, properties) -> {
            WxPay wxPay = new WxPay();
            wxPay.setWxPayProperties(properties);

            WxPayConfig wxPayConfig = new WxPayConfig();
            wxPayConfig.setAppId(properties.getAppId());
            wxPayConfig.setMchId(properties.getMchId());
            wxPayConfig.setMchKey(properties.getMchKey());
            wxPayConfig.setKeyPath(properties.getKeyPath());
            WxPayService wxPayService = new WxPayServiceImpl();
            wxPayService.setConfig(wxPayConfig);

            wxPay.setWxPayService(wxPayService);
            map.put(s,wxPay);
        });
        wxPayMap.setMap(map);
        return wxPayMap;
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(name = "pay.single.ali-enabled",havingValue = "true")
    public AliPay aliPay(PaySingleProperties paySingleProperties){
        AliPay aliPay = new AliPay();
        AliPayProperties properties = paySingleProperties.getAli();
        aliPay.setAliPayProperties(properties);

        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", properties.getAppId(), properties.getPrivateKey(), "json", "utf-8", properties.getPublicKey(), "RSA2");
        aliPay.setAlipayClient(alipayClient);
        return aliPay;
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(name = "pay.map.ali-enabled",havingValue = "true")
    public AliPayMap aliPayMap(PayMapProperties payMapProperties){
        AliPayMap aliPayMap = new AliPayMap();
        Map<String, AliPayProperties> ali = payMapProperties.getAli();
        Map<String, AliPay> map = new HashMap<>(ali.size());
        ali.forEach((s, properties) -> {
            AliPay aliPay = new AliPay();
            aliPay.setAliPayProperties(properties);

            AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", properties.getAppId(), properties.getPrivateKey(), "json", "utf-8", properties.getPublicKey(), "RSA2");
            aliPay.setAlipayClient(alipayClient);
            map.put(s,aliPay);
        });
        aliPayMap.setMap(map);
        return aliPayMap;
    }

}
